/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/

package com.jinhe.tss.core.cachepool.threadpool;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

import com.jinhe.tss.core.cachepool.extend.assignment.IAssignment;


/** 
 * <p> Scanner.java </p>
 * 扫描机器的各个端口，判断各端口是开着还是关闭的。 
 * 
 * @author 金普俊 2007-1-9
 *
 */
public class _Scanner implements IAssignment {
    String host = "";
    int    port;
    long   createTime = System.currentTimeMillis();
    String oldInfo = "";
    
    public _Scanner(){
        try {
            Thread.sleep(2); //休眠n(ms)，增加对象的创建时间
        } catch (InterruptedException e) {
        }
    }

    public void excute() {
        Socket s = null;
        try {
            if(port > 0 && port % 5000 == 0) {
                System.out.println("已经扫描 " + port + " 个");
            }
            
            s = new Socket(InetAddress.getByName(host), port);
            System.out.println("****** " + this + " ****** : " + " is opened.");
        } catch (IOException ex) {
            System.out.println("****** " + this + " ****** : " + " is closed.");
        } finally {
            try {
                if (s != null) s.close();
            } catch (IOException e) {
            }
        }
    }
    
    public int hashCode(){
        return new Long(createTime).hashCode() * new Long(System.currentTimeMillis()).hashCode();
    }
    
    public boolean equals(Object o){
        if(o instanceof _Scanner){
            _Scanner temp = (_Scanner) o;
            return this.createTime == temp.createTime;
        }
        return false;
    }
    
    public String toString(){
        return "(host : " + this.host + " , port : " + this.port + ", was(" + oldInfo + "))"; 
    }

    public void recycle() throws Exception {
        oldInfo = "(host : " + this.host + " , port : " + this.port + ")";
        this.host = "";
        this.port = 0;
    }
 
    public void setHost(String host) {
        this.host = host;
    }
 
    public void setPort(int port) {
        this.port = port;
    }
}

